<?php

namespace plugin\ycMassage\app\api\controller;

use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageAlarm;
use plugin\ycMassage\app\model\MassageMch;
use plugin\ycMassage\app\model\MassageShop;
use plugin\ycMassage\app\model\MassageUser;
use plugin\ycMassage\app\model\MassageUserTechnician;
use plugin\ycMassage\utils\enum\NoticeType;
use plugin\ycMassage\utils\notice\Notice;
use support\Request;
use think\facade\Db;

class AlarmController extends BaseController
{
    public function create(Request $request)
    {
        $D = $request->post();
        $D['uid'] = $request->uid;
        $D['saas_appid'] = $request->saas_appid;
        $model = new MassageAlarm;
        Db::startTrans();
        try {
            if($D['use_role']==='technician'){
                $MassageUserTechnician=MassageUserTechnician::where(['uid'=>$request->uid,'saas_appid'=>$request->saas_appid])->find();
                if($MassageUserTechnician){
                    $D['mch_id']=$MassageUserTechnician->work_mch_id;
                    $D['shop_id']=$MassageUserTechnician->work_shop_id;
                }
            }
            $model->save($D);
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            return $this->fail($th->getMessage());
        }
        $notice = new Notice();
        $notice->saas_appid = $request->saas_appid;
        $notice->type = NoticeType::ALARM['value'];
        $notice->role = ['ADMIN','USER'];
        $role='客户';
        if($D['use_role']==='technician'){
            $role='技师';
            $MassageUserTechnician=MassageUserTechnician::where(['uid'=>$request->uid,'saas_appid'=>$request->saas_appid])->find();
            if($MassageUserTechnician){
                $MassageMch=MassageMch::where(['id'=>$MassageUserTechnician->work_mch_id])->find();
                if($MassageMch){
                    $MassageUser = MassageUser::alias('u')->where(['u.id' => $MassageMch->technician_id])
                        ->join('MassageUserWechat uw', 'uw.uid=u.id', 'left')
                        ->field('u.nickname,u.mobile,uw.openid')
                        ->find();
                    if ($MassageUser) {
                        $notice->addUSER($MassageUser->mobile, $MassageUser->openid);
                    }
                }
                $MassageShop=MassageShop::where(['id'=>$MassageUserTechnician->work_shop_id])->find();
                if($MassageShop){
                    $MassageUser = MassageUser::alias('u')->where(['u.id' => $MassageShop->uid])
                        ->join('MassageUserWechat uw', 'uw.uid=u.id', 'left')
                        ->field('u.nickname,u.mobile,uw.openid')
                        ->find();
                    if ($MassageUser) {
                        $notice->addUSER($MassageUser->mobile, $MassageUser->openid);
                    }
                }
            }
        }
        $MassageUser = MassageUser::where(['id' => $model->uid])->find();
        $notice->setData([
            'role' => $role,
            'service_name' => "{$role}一键报警",
            'name' => $MassageUser->nickname . '(' . $model->uid . ')',
            'time' => date('Y-m-d H:i:s'),
        ]);
        $notice->send();
        return $this->success('已通知管理员，请保持手机畅通');
    }
}
